package ch.zbw.swt.sysventory.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import IMG.GetPath;
import java.awt.GridLayout;
import javax.swing.JScrollPane;
import javax.swing.JList;
import java.awt.BorderLayout;
import javax.swing.AbstractListModel;
import javax.swing.event.ListSelectionListener;

/*
 * GUI zeigt das Suchmenu an
 */
public class GUISucheV2 extends JFrame{

	//Panels
	private JPanel panel;
	private JPanel panel_1;
	private JPanel panel_2;
	private JPanel panel_3;
	private JPanel mainPanel;
	private JPanel labelPanel;
	
	private JScrollPane scrollPane1;
	private JScrollPane scrollPane2;
	private JScrollPane scrollPane3;
	
	//Bilder
	private ImageIcon goArrow;
	private ImageIcon backArrow;

	//Labels
	private JLabel titelMenuLabel;
	private JLabel computerLabel;
	private JLabel versionLabel;
	private JLabel kriteriumLabel ;
	
	//Buttons
	private JButton go2SearchResultButton;
	private JButton back2MenuButton;
	
	//Liste
	private JList computerList;
	private JList versionList;
	private JList kategorieList;
	
	//Listener
	private Action action;
	private ListSelectionListener listener;
	
	/**
	 * Konstruktor der GUI
	 */
	public GUISucheV2(Action action, ListSelectionListener listener) {
		this.action = action;
		this.listener = listener;
	}

	/**
	 * Initialisieren von GUI
	 */
	public void initialize() {
		
		//Eingschaften von GUI
		this.getContentPane().setBackground(Color.WHITE);
		this.setBackground(Color.WHITE);
		this.setBounds(100, 100, 770, 480);
		
		//Eimittung
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int top = (screenSize.height - this.getHeight()) / 2;
		int left = (screenSize.width - this.getWidth()) / 2;
		this.setLocation(left, top);
		
		//weitere Eigenschaften von GUI
		this.setResizable(false);
		this.setVisible(true);
		
		//Einfügen von Elemente
		panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setBounds(10, 11, 744, 83);
		this.getContentPane().add(panel);
		panel.setLayout(null);
		
		goArrow = new ImageIcon(GetPath.class.getResource("go.png"));			
		backArrow = new ImageIcon(GetPath.class.getResource("back.png"));
		
		titelMenuLabel = new JLabel("Inventursuche");
		titelMenuLabel.setFont(new Font("Impact", Font.PLAIN, 36));
		titelMenuLabel.setBounds(256, 11, 316, 61);
		panel.add(titelMenuLabel);
		
		go2SearchResultButton = new JButton(goArrow);
		go2SearchResultButton.setBackground(Color.WHITE);
		go2SearchResultButton.setBounds(592, 11, 129, 61);
		go2SearchResultButton.setBorder(null);
		panel.add(go2SearchResultButton);
		
		
		back2MenuButton = new JButton(backArrow);
		back2MenuButton.setBackground(Color.WHITE);
		back2MenuButton.setBounds(24, 11, 129, 61);
		back2MenuButton.setBorder(null);
		panel.add(back2MenuButton);
		
		mainPanel = new JPanel();
		mainPanel.setBounds(10, 160, 744, 241);
		panel.add(mainPanel);
		mainPanel.setLayout(new GridLayout(0, 3, 5, 0));
		
		panel_1 = new JPanel();
		mainPanel.add(panel_1);
		panel_1.setLayout(new BorderLayout(4, 2));
		
		scrollPane1 = new JScrollPane();
		panel_1.add(scrollPane1);
		
		computerList = new JList();
		computerList.setSelectionMode(0);
		
		computerList.addListSelectionListener(listener);
		
		computerList.setModel(new AbstractListModel() {
			String[] values = new String[] {"PC1", "PC2", "PC3", "PC4", "PC5", "PC6", "PC7", "PC8", "PC9", "PC10", "PC11", "PC12", "PC13", "PC14", "PC15", "PC16", "PC17"};
			public int getSize() {
				return values.length;
			}
			public Object getElementAt(int index) {
				return values[index];
			}
		});
		scrollPane1.setViewportView(computerList);
		
		panel_2 = new JPanel();
		mainPanel.add(panel_2);
		panel_2.setLayout(new BorderLayout(2, 0));
		
		scrollPane2 = new JScrollPane();
		panel_2.add(scrollPane2, BorderLayout.CENTER);
		
		versionList = new JList();
		versionList.setSelectionMode(0);
		versionList.setModel(new AbstractListModel() {
			String[] values = new String[] {"agfdg", "dsfgsdfg", "dsfgsdfgsd", "<SFDSDF", "SG", "XFBV", "CX", "BV", "XCV", "XC", "V", "XCV", "XCV", "X", "V", "XCBHDFB", " CB"};
			public int getSize() {
				return values.length;
			}
			public Object getElementAt(int index) {
				return values[index];
			}
		});
		scrollPane2.setViewportView(versionList);
		
		panel_3 = new JPanel();
		mainPanel.add(panel_3);
		panel_3.setLayout(new BorderLayout(0, 0));
		
		scrollPane3 = new JScrollPane();
		panel_3.add(scrollPane3, BorderLayout.CENTER);
		
		kategorieList = new JList();
		kategorieList.setModel(new AbstractListModel() {
			String[] values = new String[] {"IP", "Prozessortyp", "Computertyp", "Betriebssystem"};
			public int getSize() {
				return values.length;
			}
			public Object getElementAt(int index) {
				return values[index];
			}
		});
		scrollPane3.setViewportView(kategorieList);
		
		labelPanel = new JPanel();
		labelPanel.setBackground(Color.WHITE);
		labelPanel.setBounds(10, 83, 744, 66);
		panel.add(labelPanel);
		labelPanel.setLayout(new GridLayout(0, 3, 5, 0));
		
		computerLabel = new JLabel("Computer");
		computerLabel.setBackground(Color.WHITE);
		computerLabel.setFont(new Font("Impact", Font.PLAIN, 16));
		labelPanel.add(computerLabel);
		
		versionLabel = new JLabel("Version");
		versionLabel.setBackground(Color.WHITE);
		versionLabel.setFont(new Font("Impact", Font.PLAIN, 16));
		labelPanel.add(versionLabel);
		
		kriteriumLabel = new JLabel("Kriterium (max. 8)");
		kriteriumLabel.setFont(new Font("Impact", Font.PLAIN, 16));
		labelPanel.add(kriteriumLabel);
		
		
		//Einfügen von Actionlistener
		go2SearchResultButton.addActionListener(action);
		go2SearchResultButton.setActionCommand("go2searchResult");
		back2MenuButton.addActionListener(action);
		back2MenuButton.setActionCommand("back2Menu");

	}
	
	/*
	 * Für der Ermittlung von den vorhanden Versionen wird bei auswahl
	 * des Rechners ein Event ausgelöst bei dem der Rechnername ermittelt
	 * und die Liste der Version erneuert wird.
	 */
	public String getComputerName() {
		return (String) computerList.getSelectedValue();
	}
}
